/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.action.style;

import java.util.EventObject;

import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.reportgear.core.util.ImageUtils;
import com.reportgear.designer.action.UndoableAction;
import com.reportgear.designer.pane.style.BorderStylePane;
import com.reportgear.designer.pane.style.BorderStyleSelectPane;
import com.reportgear.report.model.cell.style.BorderStyle;
import com.reportgear.report.view.JWorksheet;

/**
 * 边框设计Action
 * 
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class BorderAction extends UndoableAction implements ChangeListener {

	public BorderAction() {
		this.setName("边框");
	}

	@Override
	public boolean executeAction(EventObject evt) {
		Object o = getValue(BorderStyleSelectPane.class.getName());
		JWorksheet worksheet = getWorkSheet();
		if (worksheet == null) {
			return false;
		}

		if ((o != null) && (o instanceof BorderStyleSelectPane)) {
			BorderStylePane borderPane = new BorderStylePane();
			BorderStyle cellBorderStyle = ((BorderStyleSelectPane) o).getCellBorderStyle();
			borderPane.setCellBorderStyle(cellBorderStyle);

			borderPane.updateCellStyle(worksheet);
			return true;
		}
		return false;
	}

	@Override
	public JComponent createToolBarComponent() {
		Object o = getValue(BorderStyleSelectPane.class.getName());

		if ((o == null) || (!(o instanceof BorderStyleSelectPane))) {
			BorderStyleSelectPane pane = new BorderStyleSelectPane(25, 24, ImageUtils
					.readIcon("/resources/images/format/noboder.png"));
			putValue(BorderStyleSelectPane.class.getName(), pane);
			pane.setEnabled(isEnabled());
			pane.setToolTipText(createButtonToolTipText(this));
			pane.setCellBorderStyle(new BorderStyle());
			pane.addStyleChangeListener(this);

			return pane;
		}

		return ((JComponent) o);
	}

	public void stateChanged(ChangeEvent evt) {
		executeUndoAction(evt);
	}
}